package pl.iachoo.ctl.tabory.ui.editors.compositions;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.Section;

import pl.iachoo.ctl.tabory.dao.api.services.ICompositionService;
import pl.iachoo.ctl.tabory.dao.api.services.IVagonService;
import pl.iachoo.ctl.tabory.dao.hibernate.services.ServiceManager;
import pl.iachoo.ctl.tabory.ui.base.EditorStateSL;
import pl.iachoo.ctl.tabory.ui.base.masterdetail.AbstractMasterDetailSection;
import pl.iachoo.ctl.tabory.ui.properties.Messages;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionListener;

public class CompositionEditor
		extends
		AbstractMasterDetailSection<CompositionSelection, CompositionMasterSectionPart, CompositionDetailSectionPart> {

	public static final String ID = "pl.iachoo.ctl.tabory.ui.editors.compositions.CompositionEditor";

	private final static Logger logger = Logger
			.getLogger(CompositionEditor.class);

	public CompositionEditor() throws Exception {
		super();
	}

	@Override
	protected CompositionMasterSectionPart getMasterSection(Form parentForm) {
		CompositionMasterSectionPart sectionPart = new CompositionMasterSectionPart(
				parentForm.getBody(), formToolkit, Section.TITLE_BAR,
				Messages.getString("compositions.master.title"), true, true);
		sectionPart.setSelection(selection);
		return sectionPart;
	}

	@Override
	protected CompositionDetailSectionPart getDetailSection(Form parentForm) {
		CompositionDetailSectionPart sectionPart = new CompositionDetailSectionPart(
				parentForm.getBody(), formToolkit, Section.TITLE_BAR,
				Messages.getString("compositions.details.title"));
		sectionPart.setSelection(selection);
		return sectionPart;
	}

	@Override
	protected void postInitGui(Composite parent) {
		masterSectionPart
				.addSelectionListener(new ISelectionListener<CompositionSelection>() {
					@Override
					public void selectionChanged(CompositionSelection selection) {
						logger.debug("Selection changed");
						EditorStateSL editorStateSL = selection
								.getEditorState();
						if (editorStateSL.equals(EditorStateSL.CLEAN)) {
							setDirty(false);
						} else {
							setDirty(true);
						}
						masterSectionPart.setSelection(selection);
						detailSectionPart.setSelection(selection);
					}
				});
	}

	@Override
	protected void doSave() throws Exception {
		ICompositionService compositionService = ServiceManager
				.getCompositionService();
		IVagonService vagonService = ServiceManager.getVagonService();
		CompositionSelection selection = detailSectionPart.getSelection();
		compositionService.saveUpdateComposition(selection
				.getSelectedComposition());
		vagonService.updateList(selection.getListAllVagons());
		selection = initSelection();
		masterSectionPart.setSelection(selection);
		detailSectionPart.setSelection(selection);
		setDirty(false);
	}

	@Override
	protected void doCancel() throws Exception {
		selection = initSelection();
		masterSectionPart.setSelection(selection);
		detailSectionPart.setSelection(selection);
		setDirty(false);
	}

	@Override
	protected CompositionSelection initSelection() throws Exception {
		logger.debug("Creating selection");
		ICompositionService compositionService = ServiceManager
				.getCompositionService();
		IVagonService vagonService = ServiceManager.getVagonService();
		CompositionSelection selection = new CompositionSelection();
		selection.setEditorState(EditorStateSL.CLEAN);
		selection.setListAllVagons(vagonService.getListAllVagons());
		selection.setListComposition(compositionService
				.getListAllCompositions());
		logger.debug("Selection created");
		return selection;
	}

	@Override
	public void setFocus() {
		editorCancelAction.run();
	}

}
